//
//  StarView.m
//  UIH03
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StarView.h"

@implementation StarView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id) initWithFrame:(CGRect)frame number:(NSInteger)number{

    self = [super initWithFrame:frame];
    if (self) {
        self.number = number;
        [self setStar:frame];
    }
    
    return self;
}

- (void)setStar:(CGRect)frame{

    for (int i = 0; i < self.number; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(0 + (frame.size.height + 5) * i, 0, self.frame.size.height, self.frame.size.height);
        [but setBackgroundImage:[UIImage imageNamed:@"tripview_bookmark2"] forState:UIControlStateNormal];
        but.tag = 20 + i;
//        [but addTarget:self action:@selector(actionListener:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:but];
    }
}

- (void)setStarWithNumber:(NSInteger)num {

    for (int i = 0; i < num; i++) {

        [(UIButton *)[self viewWithTag:20 + i] setBackgroundImage:[UIImage imageNamed:@"tripview_bookmark2_select"] forState:UIControlStateNormal];

    }
}

- (void)actionListener:(UIButton *)button{

    for (int i = 0; i < self.number; i++) {
        if (i + 20 <= button.tag) {
            [(UIButton *)[self viewWithTag:20 + i] setBackgroundImage:[UIImage imageNamed:@"tripview_bookmark2"] forState:UIControlStateNormal];
        }
        else{
            [(UIButton *)[self viewWithTag:20 + i] setBackgroundImage:[UIImage imageNamed:@"tripview_bookmark2_select"] forState:UIControlStateNormal];
        }
    }
}

@end
